31 #define LIBSPOTON_VERSION 0x000106
32 #define LIBSPOTON_VERSION_STR "0.1.6"
34 #ifdef LIBSPOTON_OS_WINDOWS
36 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
42 #include <arpa/inet.h>
44 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
46 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
62 LIBSPOTON_ERROR_NONE = 0,
63 LIBSPOTON_ERROR_CALLOC,
64 LIBSPOTON_ERROR_GCRY_CALLOC,
65 LIBSPOTON_ERROR_GCRY_CALLOC_SECURE,
66 LIBSPOTON_ERROR_GCRY_CHECK_VERSION,
67 LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT,
68 LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN,
69 LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN,
70 LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME,
71 LIBSPOTON_ERROR_GCRY_CIPHER_OPEN,
72 LIBSPOTON_ERROR_GCRY_CIPHER_SETIV,
73 LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY,
74 LIBSPOTON_ERROR_GCRY_CONTROL,
75 LIBSPOTON_ERROR_GCRY_KDF_DERIVE,
76 LIBSPOTON_ERROR_GCRY_MD_MAP_NAME,
77 LIBSPOTON_ERROR_INVALID_LENGTH,
78 LIBSPOTON_ERROR_INVALID_PARAMETER,
79 LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED,
80 LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE,
81 LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE,
82 LIBSPOTON_ERROR_NUMERIC_ERROR,
83 LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT,
84 LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION,
85 LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE,
86 LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL,
87 LIBSPOTON_ERROR_SQLITE_BIND_INT,
88 LIBSPOTON_ERROR_SQLITE_BIND_INT64,
89 LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT,
90 LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE,
91 LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER,
92 LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE,
93 LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED,
94 LIBSPOTON_ERROR_SQLITE_OPEN_V2,
95 LIBSPOTON_ERROR_SQLITE_PREPARE_V2,
96 LIBSPOTON_ERROR_SQLITE_STEP
97 } libspoton_error_code_t;
102 int m_cipherAlgorithm;
104 sqlite3 *m_sqliteHandle;
107 typedef libspoton_error_code_t libspoton_error_t;
110 static bool libspoton_gcryctl_set_thread_cbs_set =
false;
111 static const char *libspoton_error_strings[] =
113 "LIBSPOTON_ERROR_NONE",
114 "LIBSPOTON_ERROR_CALLOC",
115 "LIBSPOTON_ERROR_CALLOC_SECURE",
116 "LIBSPOTON_ERROR_GCRY_CALLOC",
117 "LIBSPOTON_ERROR_GCRY_CHECK_VERSION",
118 "LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT",
119 "LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN",
120 "LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN",
121 "LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME",
122 "LIBSPOTON_ERROR_GCRY_CIPHER_OPEN",
123 "LIBSPOTON_ERROR_GCRY_CIPHER_SETIV",
124 "LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY",
125 "LIBSPOTON_ERROR_GCRY_CONTROL",
126 "LIBSPOTON_ERROR_GCRY_KDF_DERIVE",
127 "LIBSPOTON_ERROR_GCRY_MD_MAP_NAME",
128 "LIBSPOTON_ERROR_INVALID_LENGTH",
129 "LIBSPOTON_ERROR_INVALID_PARAMETER",
130 "LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED",
131 "LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE",
132 "LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE",
133 "LIBSPOTON_ERROR_NUMERIC_ERROR",
134 "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT",
135 "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION",
136 "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE",
137 "LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL",
138 "LIBSPOTON_ERROR_SQLITE_BIND_INT",
139 "LIBSPOTON_ERROR_SQLITE_BIND_INT64",
140 "LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT",
141 "LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE",
142 "LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER",
143 "LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE",
144 "LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED",
145 "LIBSPOTON_ERROR_SQLITE_OPEN_V2",
146 "LIBSPOTON_ERROR_SQLITE_PREPARE_V2",
147 "LIBSPOTON_ERROR_SQLITE_STEP"
149 static libspoton_error_t libspoton_error_maximum_error =
150 LIBSPOTON_ERROR_SQLITE_STEP;
151 static pthread_mutex_t sqlite_mutex = PTHREAD_MUTEX_INITIALIZER;
153 #if !defined(GCRYPT_VERSION_NUMBER) || GCRYPT_VERSION_NUMBER < 0x010600
154 GCRY_THREAD_OPTION_PTHREAD_IMPL;
157 static inline libspoton_error_t initialize_libgcrypt
158 (
const int secure_memory_pool_size)
164 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
166 if(!libspoton_gcryctl_set_thread_cbs_set)
168 gcry_error_t err = 0;
170 #if !defined(GCRYPT_VERSION_NUMBER) || GCRYPT_VERSION_NUMBER < 0x010600
171 err = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread, 0);
175 libspoton_gcryctl_set_thread_cbs_set =
true;
177 fprintf(stderr,
"libspoton::initialize_libgcrypt(): "
178 "error initializing threads. Proceeding.\n");
181 if(!gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
183 gcry_control(GCRYCTL_ENABLE_M_GUARD);
185 if(!gcry_check_version(GCRYPT_VERSION))
186 rerr = LIBSPOTON_ERROR_GCRY_CHECK_VERSION;
187 else if(secure_memory_pool_size == 0)
189 gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
190 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
194 gcry_control(GCRYCTL_SUSPEND_SECMEM_WARN);
195 #ifdef LIBSPOTON_IGNORE_GCRY_CONTROL_GCRYCTL_INIT_SECMEM_RETURN_VALUE
196 gcry_control(GCRYCTL_INIT_SECMEM, abs(secure_memory_pool_size), 0);
198 if(gcry_control(GCRYCTL_INIT_SECMEM,
199 abs(secure_memory_pool_size), 0) != 0)
200 rerr = LIBSPOTON_ERROR_GCRY_CONTROL;
202 gcry_control(GCRYCTL_RESUME_SECMEM_WARN);
203 gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
215 libspoton_error_t *error);
221 const char *libspoton_strerror(
const libspoton_error_t error);
223 libspoton_error_t libspoton_create_urls_table
231 const int encrypted);
237 libspoton_error_t libspoton_deregister_kernel
244 libspoton_error_t libspoton_init_a(
const char *databasePath,
245 const char *cipherType,
247 const size_t keyLength,
249 const int secure_memory_pool_size);
250 libspoton_error_t libspoton_init_b(
const char *databasePath,
251 const char *cipherType,
252 const char *hashType,
253 const char *passphrase,
255 const size_t passphraseLength,
256 const size_t saltLength,
257 const unsigned long iterationCount,
259 const int secure_memory_pool_size);
265 libspoton_error_t libspoton_register_kernel
267 const bool forceRegistration,
278 libspoton_error_t libspoton_save_url(
const char *url,
279 const size_t urlSize,
281 const size_t titleSize,
282 const char *description,
283 const size_t descriptionSize,
285 const size_t contentSize,
293 libspoton_error_t *error);
300 void libspoton_enable_sqlite_cache(
void);
302 inline bool libspoton_is_kernel_registered
305 return libspoton_registered_kernel_pid(libspotonHandle, error) > 0;
308 inline const char *libspoton_strerror(
const libspoton_error_t error)
310 if(error > libspoton_error_maximum_error)
313 return libspoton_error_strings[error];
316 inline libspoton_error_t libspoton_create_urls_table
320 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
324 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
327 else if(!libspotonHandle->m_sqliteHandle)
329 rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
333 pthread_mutex_lock(&sqlite_mutex);
334 rv = sqlite3_exec(libspotonHandle->m_sqliteHandle,
335 "CREATE TABLE IF NOT EXISTS urls ("
338 "encrypted INTEGER NOT NULL DEFAULT 0, "
340 "url BLOB PRIMARY KEY NOT NULL)",
344 pthread_mutex_unlock(&sqlite_mutex);
348 rerr = LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE;
356 inline libspoton_error_t libspoton_delete_urls
359 const char *sql =
"DELETE FROM urls WHERE encrypted = ?";
361 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
362 sqlite3_stmt *stmt = 0;
366 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
369 else if(!libspotonHandle->m_sqliteHandle)
371 rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
375 pthread_mutex_lock(&sqlite_mutex);
376 rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
381 pthread_mutex_unlock(&sqlite_mutex);
385 rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
389 if(sqlite3_bind_int(stmt, 1, encrypted) != SQLITE_OK)
391 rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT;
395 rv = sqlite3_step(stmt);
397 if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
399 rerr = LIBSPOTON_ERROR_SQLITE_STEP;
404 sqlite3_finalize(stmt);
408 inline libspoton_error_t libspoton_deregister_kernel
411 const char *sql =
"DELETE FROM kernel_registration WHERE pid = ?";
413 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
414 sqlite3_stmt *stmt = 0;
418 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
421 else if(!libspotonHandle->m_sqliteHandle)
423 rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
427 pthread_mutex_lock(&sqlite_mutex);
428 rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
433 pthread_mutex_unlock(&sqlite_mutex);
437 rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
441 if(sqlite3_bind_int64(stmt, 1, (sqlite3_int64) pid) != SQLITE_OK)
443 rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT64;
447 rv = sqlite3_step(stmt);
449 if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
451 rerr = LIBSPOTON_ERROR_SQLITE_STEP;
456 sqlite3_finalize(stmt);
460 inline libspoton_error_t libspoton_init_a(
const char *databasePath,
461 const char *cipherType,
463 const size_t keyLength,
465 const int secure_memory_pool_size)
468 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
472 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
476 libspotonHandle->m_cipherAlgorithm = 0;
477 libspotonHandle->m_key = 0;
478 libspotonHandle->m_keyLength = 0;
479 libspotonHandle->m_sqliteHandle = 0;
485 if((rerr = initialize_libgcrypt(secure_memory_pool_size)) !=
486 LIBSPOTON_ERROR_NONE)
489 if(key && keyLength > 0)
491 if((libspotonHandle->m_cipherAlgorithm =
492 gcry_cipher_map_name(cipherType)) == 0)
494 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME;
498 libspotonHandle->m_keyLength = keyLength;
500 if(!(libspotonHandle->m_key = (
char *)
501 gcry_calloc_secure(libspotonHandle->m_keyLength,
sizeof(
char))))
503 rerr = LIBSPOTON_ERROR_GCRY_CALLOC_SECURE;
507 memcpy(libspotonHandle->m_key, key, keyLength);
514 pthread_mutex_lock(&sqlite_mutex);
515 rv = sqlite3_open_v2(databasePath,
516 &libspotonHandle->m_sqliteHandle,
517 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
519 pthread_mutex_unlock(&sqlite_mutex);
523 rerr = LIBSPOTON_ERROR_SQLITE_OPEN_V2;
529 if(rerr != LIBSPOTON_ERROR_NONE)
530 libspoton_close(libspotonHandle);
535 inline libspoton_error_t libspoton_init_b(
const char *databasePath,
536 const char *cipherType,
537 const char *hashType,
538 const char *passphrase,
540 const size_t passphraseLength,
541 const size_t saltLength,
542 const unsigned long iterationCount,
544 const int secure_memory_pool_size)
546 int hashAlgorithm = 0;
548 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
552 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
556 libspotonHandle->m_cipherAlgorithm = 0;
557 libspotonHandle->m_key = 0;
558 libspotonHandle->m_keyLength = 0;
559 libspotonHandle->m_sqliteHandle = 0;
566 initialize_libgcrypt(secure_memory_pool_size)) != LIBSPOTON_ERROR_NONE)
569 if(cipherType && hashType && iterationCount > 0 &&
570 passphrase && salt && passphraseLength > 0 && saltLength > 0)
572 if((libspotonHandle->m_cipherAlgorithm =
573 gcry_cipher_map_name(cipherType)) == 0)
575 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_MAP_NAME;
579 if((hashAlgorithm = gcry_md_map_name(hashType)) == 0)
581 rerr = LIBSPOTON_ERROR_GCRY_MD_MAP_NAME;
585 if((libspotonHandle->m_keyLength =
586 gcry_cipher_get_algo_keylen(libspotonHandle->
587 m_cipherAlgorithm)) == 0)
589 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_KEYLEN;
593 if(!(libspotonHandle->m_key = (
char *)
594 gcry_calloc_secure(libspotonHandle->m_keyLength,
sizeof(
char))))
596 rerr = LIBSPOTON_ERROR_GCRY_CALLOC_SECURE;
600 gcry_fast_random_poll();
602 if(gcry_kdf_derive((
const void *) passphrase,
609 libspotonHandle->m_keyLength,
610 (
void *) libspotonHandle->m_key) != 0)
612 rerr = LIBSPOTON_ERROR_GCRY_KDF_DERIVE;
621 pthread_mutex_lock(&sqlite_mutex);
622 rv = sqlite3_open_v2(databasePath,
623 &libspotonHandle->m_sqliteHandle,
624 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
626 pthread_mutex_unlock(&sqlite_mutex);
630 rerr = LIBSPOTON_ERROR_SQLITE_OPEN_V2;
636 if(rerr != LIBSPOTON_ERROR_NONE)
637 libspoton_close(libspotonHandle);
642 inline libspoton_error_t libspoton_register_kernel
644 const bool forceRegistration,
647 const char *sql =
"INSERT OR REPLACE INTO kernel_registration (pid) "
650 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
651 sqlite3_stmt *stmt = 0;
655 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
658 else if(!libspotonHandle->m_sqliteHandle)
660 rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
664 pthread_mutex_lock(&sqlite_mutex);
665 rv = sqlite3_exec(libspotonHandle->m_sqliteHandle,
666 "CREATE TABLE IF NOT EXISTS kernel_registration ("
667 "pid INTEGER PRIMARY KEY NOT NULL)",
671 pthread_mutex_unlock(&sqlite_mutex);
675 rerr = LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TABLE;
683 pthread_mutex_lock(&sqlite_mutex);
685 (libspotonHandle->m_sqliteHandle,
686 "CREATE TRIGGER IF NOT EXISTS kernel_registration_trigger "
687 "BEFORE INSERT ON kernel_registration "
689 "DELETE FROM kernel_registration; "
694 pthread_mutex_unlock(&sqlite_mutex);
698 rerr = LIBSPOTON_ERROR_SQLITE_CREATE_KERNEL_REGISTRATION_TRIGGER;
702 if(libspoton_registered_kernel_pid(libspotonHandle, &rerr) > 0)
703 if(!forceRegistration)
705 rerr = LIBSPOTON_ERROR_KERNEL_PROCESS_ALREADY_REGISTERED;
709 if(rerr != LIBSPOTON_ERROR_NONE)
712 pthread_mutex_lock(&sqlite_mutex);
713 rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
718 pthread_mutex_unlock(&sqlite_mutex);
722 rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
726 if(sqlite3_bind_int64(stmt, 1, (sqlite3_int64) pid) != SQLITE_OK)
728 rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT64;
732 rv = sqlite3_step(stmt);
734 if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
736 rerr = LIBSPOTON_ERROR_SQLITE_STEP;
741 sqlite3_finalize(stmt);
745 inline libspoton_error_t libspoton_save_url
747 const size_t urlSize,
749 const size_t titleSize,
750 const char *description,
751 const size_t descriptionSize,
753 const size_t contentSize,
757 char *encodedBuffer = 0;
758 char *encodedBufferAndIV = 0;
761 const char *buffer =
"";
762 const char *sql =
"INSERT OR REPLACE INTO urls (url, title, "
763 "description, content, encrypted) VALUES (?, ?, ?, ?, ?)";
764 int FIELDS_COUNT = 4;
765 gcry_cipher_hd_t cipherCtx = 0;
768 libspoton_error_t rerr = LIBSPOTON_ERROR_NONE;
769 size_t blockLength = 0;
770 size_t encodedBufferAndIVLength = 0;
771 size_t encodedBufferLength = 0;
773 size_t sizeofchar =
sizeof(char);
774 sqlite3_stmt *stmt = 0;
778 rerr = LIBSPOTON_ERROR_NULL_LIBSPOTON_HANDLE;
781 else if(!libspotonHandle->m_sqliteHandle)
783 rerr = LIBSPOTON_ERROR_NOT_CONNECTED_TO_SQLITE_DATABASE;
787 if(!libspotonHandle->m_cipherAlgorithm ||
788 !libspotonHandle->m_key || !libspotonHandle->m_keyLength)
792 if(gcry_cipher_open(&cipherCtx, libspotonHandle->m_cipherAlgorithm,
793 GCRY_CIPHER_MODE_CBC,
794 GCRY_CIPHER_CBC_CTS |
795 GCRY_CIPHER_SECURE) != 0 || !cipherCtx)
797 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_OPEN;
803 if(gcry_cipher_setkey(cipherCtx,
804 libspotonHandle->m_key,
805 libspotonHandle->m_keyLength) != 0)
807 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_SETKEY;
813 if((blockLength = gcry_cipher_get_algo_blklen(libspotonHandle->
814 m_cipherAlgorithm)) == 0)
816 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_GET_ALGO_BLKLEN;
820 if(libspoton_create_urls_table(libspotonHandle) != SQLITE_OK)
822 rerr = LIBSPOTON_ERROR_SQLITE_CREATE_URLS_TABLE;
826 pthread_mutex_lock(&sqlite_mutex);
827 rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle,
832 pthread_mutex_unlock(&sqlite_mutex);
836 rerr = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
840 for(i = 1; i <= FIELDS_COUNT; i++)
844 iv = (
char *) gcry_calloc(blockLength,
sizeof(
char));
848 #ifdef LIBSPOTON_OS_OPENBSD
849 gcry_cipher_ctl(cipherCtx, GCRYCTL_RESET, NULL, (
size_t) 0);
851 gcry_cipher_reset(cipherCtx);
853 gcry_fast_random_poll();
854 gcry_create_nonce(iv, blockLength);
856 if(gcry_cipher_setiv(cipherCtx, iv, blockLength) != 0)
858 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_SETIV;
864 rerr = LIBSPOTON_ERROR_GCRY_CALLOC;
881 buffer = description;
882 length = descriptionSize;
887 length = contentSize;
892 if(length < blockLength)
893 encodedBufferLength = blockLength;
895 encodedBufferLength = length;
897 if(SIZE_MAX - encodedBufferLength >= 4)
898 encodedBufferLength += 4;
904 rerr = LIBSPOTON_ERROR_NUMERIC_ERROR;
908 if(sizeofchar > 0 && encodedBufferLength <= SIZE_MAX / sizeofchar)
909 encodedBuffer = (
char *) calloc
910 (encodedBufferLength,
sizeof(
char));
916 rerr = LIBSPOTON_ERROR_CALLOC;
920 memcpy(encodedBuffer, buffer, length);
927 #ifdef LIBSPOTON_OS_WINDOWS
928 lengthArray[3] = (char) (length & 0xff);
929 lengthArray[2] = (char) ((length >> 8) & 0xff);
930 lengthArray[1] = (char) ((length >> 16) & 0xff);
931 lengthArray[0] = (char) ((length >> 24) & 0xff);
932 encodedBuffer[encodedBufferLength - 4] = lengthArray[0];
933 encodedBuffer[encodedBufferLength - 3] = lengthArray[1];
934 encodedBuffer[encodedBufferLength - 2] = lengthArray[2];
935 encodedBuffer[encodedBufferLength - 1] = lengthArray[3];
937 length = (size_t) htonl((uint32_t) length);
938 memcpy(lengthArray, &length, (
size_t) 4);
940 (&encodedBuffer[encodedBufferLength - 4], lengthArray, (
size_t) 4);
942 gcry_fast_random_poll();
944 if(gcry_cipher_encrypt(cipherCtx,
945 encodedBuffer, encodedBufferLength,
948 if(SIZE_MAX - blockLength >= encodedBufferLength)
949 encodedBufferAndIVLength = blockLength + encodedBufferLength;
952 rerr = LIBSPOTON_ERROR_NUMERIC_ERROR;
957 encodedBufferAndIVLength <= SIZE_MAX / sizeofchar)
958 encodedBufferAndIV = (
char *) calloc
959 (encodedBufferAndIVLength, sizeofchar);
961 encodedBufferAndIV = 0;
963 if(encodedBufferAndIV)
965 memcpy(encodedBufferAndIV, iv, blockLength);
966 memcpy(&encodedBufferAndIV[blockLength], encodedBuffer,
967 encodedBufferLength);
971 rerr = LIBSPOTON_ERROR_CALLOC;
977 rerr = LIBSPOTON_ERROR_GCRY_CIPHER_ENCRYPT;
984 if(sqlite3_bind_blob(stmt,
987 (
int) encodedBufferAndIVLength,
988 SQLITE_TRANSIENT) != SQLITE_OK)
991 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL;
993 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE;
995 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION;
997 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT;
1004 if(sqlite3_bind_blob(stmt,
1008 SQLITE_TRANSIENT) != SQLITE_OK)
1011 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_URL;
1013 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_TITLE;
1015 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_DESCRIPTION;
1017 rerr = LIBSPOTON_ERROR_SQLITE_BIND_BLOB_CONTENT;
1023 free(encodedBuffer);
1025 free(encodedBufferAndIV);
1026 encodedBufferAndIV = 0;
1031 if(sqlite3_bind_int(stmt,
1033 encrypt) != SQLITE_OK)
1035 rerr = LIBSPOTON_ERROR_SQLITE_BIND_INT_ENCRYPT;
1039 rv = sqlite3_step(stmt);
1041 if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
1043 rerr = LIBSPOTON_ERROR_SQLITE_STEP;
1047 sqlite3_finalize(stmt);
1051 free(encodedBuffer);
1052 free(encodedBufferAndIV);
1053 gcry_cipher_close(cipherCtx);
1055 sqlite3_finalize(stmt);
1059 inline pid_t libspoton_registered_kernel_pid
1063 const char *sql =
"SELECT pid FROM kernel_registration LIMIT 1";
1064 sqlite3_stmt *stmt = 0;
1065 sqlite3_int64 pid = 0;
1067 if(!libspotonHandle)
1069 else if(!libspotonHandle->m_sqliteHandle)
1072 pthread_mutex_lock(&sqlite_mutex);
1073 rv = sqlite3_prepare_v2(libspotonHandle->m_sqliteHandle, sql, -1, &stmt, 0);
1074 pthread_mutex_unlock(&sqlite_mutex);
1078 if((rv = sqlite3_step(stmt)) == SQLITE_ROW)
1079 pid = sqlite3_column_int64(stmt, 0);
1082 if(rv == SQLITE_BUSY || rv == SQLITE_LOCKED)
1083 *error = LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED;
1084 else if(!(rv == SQLITE_OK || rv == SQLITE_DONE))
1085 *error = LIBSPOTON_ERROR_SQLITE_STEP;
1090 if(rv == SQLITE_BUSY || rv == SQLITE_LOCKED)
1091 *error = LIBSPOTON_ERROR_SQLITE_DATABASE_LOCKED;
1093 *error = LIBSPOTON_ERROR_SQLITE_PREPARE_V2;
1096 sqlite3_finalize(stmt);
1106 gcry_free(libspotonHandle->m_key);
1107 pthread_mutex_lock(&sqlite_mutex);
1108 sqlite3_close(libspotonHandle->m_sqliteHandle);
1109 pthread_mutex_unlock(&sqlite_mutex);
1110 libspotonHandle->m_cipherAlgorithm = 0;
1111 libspotonHandle->m_key = 0;
1112 libspotonHandle->m_keyLength = 0;
1113 libspotonHandle->m_sqliteHandle = 0;
1117 inline void libspoton_enable_sqlite_cache(
void)
1119 #ifndef LIBSPOTON_OS_MAC
1120 #ifndef LIBSPOTON_OS_OS2
1121 sqlite3_enable_shared_cache(1);